﻿Imports Microsoft.VisualBasic
Imports Framework
Imports SchemaMikeConnellMinistries

<CLSCompliant(True)> _
Partial Public Class CSetting : Inherits SchemaMikeConnellMinistries.CSetting


#Region "Enum: Primary Key Values"
    <CLSCompliant(True)> _
    Public Enum ESetting
        FeaturedProductId = 1
        ScheduleText = 2
        PhotosHtml = 3
        AboutText = 4
        AboutImage = 5
        ScheduleImage = 6
        ShowPhotos = 7
        ShowItunesTwitterFacebook = 8
        AboutTextCharsToShow = 9
        MetaKeywords_Home = 10
        MetaDescription_Home = 11
        MetaKeywords_About = 12
        MetaDescription_About = 13
        MetaKeywords_Schedule = 14
        MetaDescription_Schedule = 15
        MetaKeywords_Photos = 16
        MetaDescription_Photos = 17
        MetaKeywords_Store = 18
        MetaDescription_Store = 19
        ItunesSummary = 20
        ItunesDescription = 21
        MetaKeywords_AllProducts = 22
        SermonImage = 23
    End Enum
#End Region


#Region "Shared"
    'Direct
    Public Shared Function FeaturedProductId() As Integer
        Return GetProductId(ESetting.FeaturedProductId)
    End Function
    Public Shared Function ScheduleText() As String
        Return GetText(ESetting.ScheduleText)
    End Function
    Public Shared Function PhotosHtml() As String
        Return GetHtml(ESetting.PhotosHtml)
    End Function
    Public Shared Function AboutText() As String
        Return GetText(ESetting.AboutText)
    End Function
    Public Shared Function AboutTextCharsToShow() As Integer
        Return GetInteger(ESetting.AboutTextCharsToShow, 600)
    End Function
    Public Shared Function AboutImage() As String
        Return GetImage(ESetting.AboutImage)
    End Function
    Public Shared Function ScheduleImage() As String
        Return GetImage(ESetting.ScheduleImage)
    End Function
    Public Shared Function ShowPhotos() As Boolean
        Return GetBoolean(ESetting.ShowPhotos)
    End Function
    Public Shared Function ShowItunesTwitterFacebook() As Boolean
        Return GetBoolean(ESetting.ShowItunesTwitterFacebook)
    End Function

    Public Shared Function SermonImage() As String
        Return GetImage(ESetting.SermonImage)
    End Function


    Public Shared Function MetaKeywords_Home() As String
        Return GetText(ESetting.MetaKeywords_Home)
    End Function
    Public Shared Function MetaDescription_Home() As String
        Return GetText(ESetting.MetaDescription_Home)
    End Function
    Public Shared Function MetaKeywords_About() As String
        Return GetText(ESetting.MetaKeywords_About)
    End Function
    Public Shared Function MetaDescription_About() As String
        Return GetText(ESetting.MetaDescription_About)
    End Function
    Public Shared Function MetaKeywords_Schedule() As String
        Return GetText(ESetting.MetaKeywords_Schedule)
    End Function
    Public Shared Function MetaDescription_Schedule() As String
        Return GetText(ESetting.MetaDescription_Schedule)
    End Function
    Public Shared Function MetaKeywords_Store() As String
        Return String.Concat(GetText(ESetting.MetaKeywords_Store), ",", CUtilities.ListToString(CProductType.Cache.Names))
    End Function
    Public Shared Function MetaDescription_Store() As String
        Return GetText(ESetting.MetaDescription_Store)
    End Function
    Public Shared Function MetaKeywords_Photos() As String
        Return GetText(ESetting.MetaKeywords_Photos)
    End Function
    Public Shared Function MetaDescription_Photos() As String
        Return GetText(ESetting.MetaDescription_Photos)
    End Function
    Public Shared Function MetaKeywords_AllProducts() As String  'odd one out, no desc
        Return GetText(ESetting.MetaKeywords_AllProducts)
    End Function



    Public Shared Function ItunesSummary() As String
        Return GetText(ESetting.ItunesSummary)
    End Function
    Public Shared Function ItunesDescription() As String
        Return GetText(ESetting.ItunesDescription)
    End Function

    'Derived
    Public Shared Function AboutTextBr() As String
        Return Br(AboutText).Replace("Bay City Outreach Centre", "<a target=""_blank"" href=""http://baycity.org.nz"" Title=""Church: Bay City Outreach"">Bay City Outreach Centre</a>")
    End Function
    Public Shared Function ScheduleTextBr() As String
        Return Br(ScheduleText)
    End Function
    Public Shared Function AboutTextBrTruncate() As String
        Return Br(CUtilities.Truncate(AboutText, AboutTextCharsToShow)) '.Replace("Bay City Outreach Centre", "<a target=""_blank"" href=""http://baycity.org.nz"" Title=""Church: Bay City Outreach"">Bay City Outreach Centre</a>")
    End Function
    Public Shared Function AboutTextBrWithLink() As String
        Return Br(AboutText).Replace("Bay City Outreach Centre", "<a target=""_blank"" href=""http://baycity.org.nz"" Title=""Church: Bay City Outreach"">Bay City Outreach Centre</a>")
    End Function
    Public Shared Function FeaturedProduct() As CProduct
        Return CProduct.Cache.GetById(FeaturedProductId)
    End Function


    'Helpers
    Public Shared Function GetImage(id As ESetting, Optional defaultVal As String = "") As String
        Return GetString(id, ESettingType.Image, defaultVal)
    End Function
    Public Shared Function GetText(id As ESetting, Optional defaultVal As String = "") As String
        Return GetString(id, ESettingType.Text, defaultVal)
    End Function
    Public Shared Function GetHtml(id As ESetting, Optional defaultVal As String = "") As String
        Return GetString(id, ESettingType.Html, defaultVal)
    End Function
    Public Shared Function GetProductId(id As ESetting) As String
        Return GetInteger(id, Integer.MinValue, ESettingType.ProductId)
    End Function

    Public Shared Function Br(s As String) As String
        If String.IsNullOrEmpty(s) Then Return s
        Return HttpUtility.HtmlEncode(s).Replace(vbCrLf, "<br/>")
    End Function


    Public Shared Function GetString(id As ESetting, type As ESettingType, defaultVal As String) As String
        Dim s As SchemaMikeConnellMinistries.CSetting = Cache.GetById(id)
        If IsNothing(s) Then
            s = New SchemaMikeConnellMinistries.CSetting
            s.SettingTypeId = type
            s.SettingId = id
            s.SettingName = id.ToString
            s.SettingString = defaultVal
            s.Save()
        ElseIf s.TypeId <> type Then
            s.TypeId = type
            s.Save()
        End If
        Return s.SettingString
    End Function
    Public Shared Function GetInteger(id As ESetting, Optional defaultVal As Integer = Integer.MinValue, Optional typeId As Byte = ESettingType.Integer) As Integer
        Dim s As SchemaMikeConnellMinistries.CSetting = Cache.GetById(id)
        If IsNothing(s) Then
            s = New SchemaMikeConnellMinistries.CSetting
            s.TypeId = ESettingType.Integer
            s.SettingId = id
            s.SettingName = id.ToString
            s.SettingInteger = defaultVal
            s.Save()
        ElseIf s.TypeId <> typeId Then
            s.TypeId = typeId
            s.Save()
        End If
        Return s.SettingInteger
    End Function
    Public Shared Function GetBoolean(id As ESetting, Optional defaultVal As Boolean? = Nothing) As Boolean?
        Dim s As SchemaMikeConnellMinistries.CSetting = Cache.GetById(id)
        If IsNothing(s) Then
            s = New SchemaMikeConnellMinistries.CSetting
            s.TypeId = ESettingType.Boolean
            s.SettingId = id
            s.SettingName = id.ToString
            s.SettingBooleanNullable = defaultVal
            s.Save()
        ElseIf s.TypeId <> ESettingType.Boolean Then
            s.TypeId = ESettingType.Boolean
            s.Save()
        End If
        Return s.SettingInteger
    End Function
#End Region

End Class
